import 'package:auto_route/auto_route.dart';
import 'package:distribution_mall_flutter/constant/constant.dart';
import 'package:distribution_mall_flutter/constant/point_constant.dart';
import 'package:distribution_mall_flutter/mixins/page_stay_time.dart';
import 'package:distribution_mall_flutter/pages/goods/product_details/application/product_details_service.dart';
import 'package:distribution_mall_flutter/pages/goods/product_details/widgets/product_address.dart';
import 'package:distribution_mall_flutter/pages/goods/product_details/widgets/product_brand.dart';
import 'package:distribution_mall_flutter/pages/goods/product_details/widgets/product_content.dart';
import 'package:distribution_mall_flutter/pages/goods/product_details/widgets/product_footer.dart';
import 'package:distribution_mall_flutter/pages/goods/product_details/widgets/product_items.dart';
import 'package:distribution_mall_flutter/pages/goods/product_details/widgets/product_nav.dart';
import 'package:distribution_mall_flutter/pages/goods/product_details/widgets/product_tag.dart';
import 'package:distribution_mall_flutter/provider/goods/product_details.dart';
import 'package:distribution_mall_flutter/router/router.dart';
import 'package:distribution_mall_flutter/utils/debug.dart';
import 'package:distribution_mall_flutter/utils/firebase/firebase_analytics_utils.dart';
import 'package:distribution_mall_flutter/utils/overlay_manager.dart';
import 'package:distribution_mall_flutter/utils/storage.dart';
import 'package:distribution_mall_flutter/widgets/pop_scope_overlay.dart';
import 'package:distribution_mall_flutter/widgets/shimmer.dart';
import 'package:easy_debounce/easy_throttle.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:webview_flutter/webview_flutter.dart';

import 'widgets/product_sample.dart';

@RoutePage()
class ProductDetailsPage extends ConsumerStatefulWidget {
  final String saleItemId;
  final String? isCode;

  const ProductDetailsPage(
      {super.key, @pathParam required this.saleItemId, @pathParam this.isCode});

  @override
  ConsumerState createState() => _ProductDetailsPage();
}

class _ProductDetailsPage extends ConsumerState<ProductDetailsPage>
    with PageStayTime<ProductDetailsPage> {
  final ScrollController _scrollController = ScrollController();
  @override
  String pageStayName = RouteInfoEnum.productDetails.routeZhName;
  @override
  Map<String, Object?> pageStayMap = {};

  @override
  void initState() {
    super.initState();
    pageStayMap['saleItemId'] = widget.saleItemId;

    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark);
    // if (kIsWeb && widget.isCode == '1') {
    //   EPstorage.route.put(RouteBoxKey.isH5PDetail, widget.saleItemId);
    // }
  }

  @override
  pageStayCallBack() {
    // 埋点用，页面被覆盖后，上报信息
    double pixels = _scrollController.position.pixels;
    if (pixels == 0) return;
    double height = EPstorage.deviceInfo.get(DeviceInfoBoxKey.height);
    FirebaseAnalyticsUtils.logEvent(
        name: PointConstant.pdPageScroll,
        parameters: {
          'pageScrollTop': pixels,
          'pageScrollScreenNum': pixels / height,
          'pageScreenHeight': height,
          'saleItemId': widget.saleItemId
        });
  }

  @override
  Widget build(BuildContext context) {
    final modal = ref.watch(pDetailProvider);
    ref.watch(productDetailsServiceProvider(widget.saleItemId));
    return PopScopeOverlay(
      child: EPShimmer(
          enabled: modal.firstLoading,
          child: Scaffold(
              backgroundColor: Colors.white,
              appBar: ProductNav(_scrollController),
              extendBodyBehindAppBar: true,
              bottomNavigationBar: ProductFooter(saleItemId: widget.saleItemId),
              body: CustomScrollView(
                controller: _scrollController,
                slivers: [
                  const ProductContent(),
                  ProductSample(widget.saleItemId),
                  const ProductItems(),
                  const ProductBrand(),
                  ProductAddress(widget.saleItemId),
                  const ProductTag(),
                  const SliverToBoxAdapter(
                      child: SizedBox(
                    height: 40,
                  ))
                ],
              ))),
    );
  }
}
